// 任务关键字
//const KEYWORD_MATCH = /^(.*浏览).*(\d+爆竹)$/;
const KEYWORD_MATCH = /.*可得(\d+爆竹)$/;
const TIME_BROWES = 10000; // 浏览时间
const TIME_WAIT = 1000; // 界面切换等待的时间
const TIME_OUT = 30; // 超时时间
// 任务类型
const TASK_TYPE = {
  Browse: 1, // 浏览
  BrowseForWhile: 2, // 浏览一段时间
  BrowseProducts: 3, // 浏览几个商品
  ReturnTwoTimes: 4, // 需要2次返回才能退出的界面
  AddMarketCar: 5, // 添加购物车
  SeeShop: 6, // 逛店铺
  JoinMember: 7, // 加入会员
  GoHome: 8, // 去首页
  ToShare: 9, // 去分享
  Shopping: 10, // 下单
  PlayGame: 11, // 玩游戏
  GotoMiniProgram: 12, // 去小程序
  JumpApp: 13 // 跳转其他App
};

let timerThread;  // 计时器线程
let timeout = 0;  // 计数

main();

function main() {
  auto.waitFor();
  //launchActivity();

  stayTimer();

  while (true) {
    checkTimeout();
    viewTask();
  }
}

function launchActivity() {
  sleep(TIME_WAIT);

  app.startActivity({
    action: "View",
    packageName: "com.jingdong.app.mall",
    className: "com.jingdong.app.mall.MainFrameActivity"
  });
}

function stayTimer() {
  timerThread = threads.start(function () {
    setInterval(function () { timeout++; }, 1000);
  });
  sleep(TIME_WAIT);
}

function checkTimeout() {
  if (timeout > TIME_OUT) {
    timerThread.interrupt();
    log("停留某个页面超过30s, 退出脚本");
    exit();
  }
}

function viewTask() {
  if (textMatches(KEYWORD_MATCH).exists()) {
    sleep(TIME_WAIT);
    let widgets = textMatches(KEYWORD_MATCH).find();
    for (let index = 0; index < widgets.length; index++) {
      let widget = widgets[index];
      let widgetParent = widget.parent();
      let progress = widgetParent.child(1).text();
      let describe = widgetParent.child(2).text();
      let num = parseInt(progress.match(/\d+(?=\/)/g));
      let total = parseInt(progress.match(/\d+(?=\))/g));
      //log(progress + " " + describe);

      if (num < total) {
        let taskType = checkTaskType(progress, describe);

        // 忽略一些任务
        if (taskType == TASK_TYPE.GoHome
          || taskType == TASK_TYPE.ToShare
          || taskType == TASK_TYPE.Shopping
          || taskType == TASK_TYPE.JumpApp) {
          continue;
        }

        let buttonWidget = widgetParent.child(3);
        //click(buttonWidget.bounds().centerX(), buttonWidget.bounds().centerY());
        if (buttonWidget.click()) {
          timeout = 0; // 点击任务 重置计时
          log("正在进行任务:" + progress.match(/.*(?=\()/) + " " + (num + 1) + "/" + total + " " + describe);
          sleep(TIME_WAIT + 3000);

          if (taskType == TASK_TYPE.Browse) {
            returnNow();
          } else if (taskType == TASK_TYPE.BrowseForWhile || taskType == TASK_TYPE.PlayGame) {
            returnLater();
          } else if (taskType == TASK_TYPE.BrowseProducts) {
            browseProducts(4);
          } else if (taskType == TASK_TYPE.ReturnTwoTimes) {
            returnTwoTimes();
          } else if (taskType == TASK_TYPE.GotoMiniProgram) {
            returnTwoTimes();
          } else if (taskType == TASK_TYPE.AddMarketCar) {
            addMarketCar(4);
          } else if (taskType == TASK_TYPE.SeeShop) {
            seeShop(3);
          } else if (taskType == TASK_TYPE.JoinMember) {
            joinMember();
          } else {
            returnNow();
          }
        }
      }
      sleep(TIME_WAIT);
    }
  }
}

function checkTaskType(progress, describe) {
  let taskType = TASK_TYPE.Browse;
  if (/浏览可得/.test(describe)) {
    if (/逛大牌店铺/.test(progress)) {
      taskType = TASK_TYPE.SeeShop;
    } else if (/会员频道/.test(progress) || /去京东金榜/.test(progress)) {
      taskType = TASK_TYPE.BrowseForWhile;
    } else if (/去瓜分3亿红包/.test(progress)) {
      taskType = TASK_TYPE.ReturnTwoTimes;
    } else {
      taskType = TASK_TYPE.Browse;
    }
  } else if (/^(浏览).*\d+(?=s)/.test(describe)) {
    taskType = TASK_TYPE.BrowseForWhile;
  } else if (/浏览\d+个商品/.test(describe)) {
    taskType = TASK_TYPE.BrowseProducts;
  } else if (/加购\d+个商品/.test(describe)) {
    taskType = TASK_TYPE.AddMarketCar;
  } else if (/下单/.test(describe)) {
    taskType = TASK_TYPE.Shopping;
  } else if (/浏览\d+个品牌墙店铺/.test(describe)) {
    taskType = TASK_TYPE.GoHome;
  } else if (/玩.*游戏/.test(describe)) {
    taskType = TASK_TYPE.PlayGame;
  } else if (/小程序/.test(describe)) {
    taskType = TASK_TYPE.GotoMiniProgram;
  } else if (/每邀\d+个好友/.test(describe)) {
    taskType = TASK_TYPE.ToShare;
  }
  return taskType;
}

function returnNow() {
  sleep(TIME_WAIT);
  back();
}

function returnTwoTimes() {
  sleep(TIME_BROWES);
  back();
  sleep(TIME_WAIT);
  back();
}

function returnThreeTimes() {
  sleep(TIME_BROWES);
  back();
  sleep(TIME_WAIT);
  back();
  sleep(TIME_WAIT);
  back();
}

function returnLater() {
  sleep(TIME_BROWES);
  back();
}

function browseProducts(count) {
  if (textContains('¥').exists()) {
    sleep(TIME_WAIT);
    let widgets = textContains('¥').find();
    for (let index = 0; index < widgets.length; index++) {
      if (index == count) {
        sleep(TIME_WAIT);
        back();
        break;
      }
      sleep(TIME_WAIT);
      let widget = widgets[index];
      let widgetParent = widget.parent().parent();
      let buttonWidget = widgetParent.child(4);
      if (buttonWidget.click()) {
        log("    浏览第" + (index + 1) + "个商品");
        sleep(TIME_WAIT + 1000);
        back();
      }
    }
  }
}

function addMarketCar(count) {
  if (textContains('¥').exists()) {
    sleep(TIME_WAIT);
    let widgets = textContains('¥').find();
    for (let index = 0; index < widgets.length; index++) {
      if (index == count) {
        sleep(TIME_WAIT);
        back();
        break;
      }
      sleep(TIME_WAIT);
      let widget = widgets[index];
      let widgetParent = widget.parent().parent();
      let buttonWidget = widgetParent.child(4);
      if (buttonWidget.click()) {
        log("    加购第" + (index + 1) + "个商品");
        sleep(TIME_WAIT + 1000);
        back();
      }
    }
  }
}

function seeShop(count) {
  if (textContains('自营').exists()) {
    sleep(TIME_WAIT);
    let widgets = textContains('自营').find();
    for (let index = 0; index < widgets.length; index++) {
      if (index == count) {
        sleep(TIME_WAIT);
        back();
        break;
      }
      sleep(TIME_WAIT);
      let widget = widgets[index];
      let widgetParent = widget.parent();
      let buttonWidget = widgetParent.child(4);
      if (buttonWidget.click()) {
        log("    逛第" + (index + 1) + "个店铺");
        sleep(TIME_WAIT + 1000);
        back();
      }
    }
  }
}

function joinMember() {
  sleep(TIME_WAIT);
  back();
}
